package cn.com.dragonsoftbravo.utils.azure.was;

import java.util.HashMap;
import java.util.Map;

import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import cn.com.dragonsoftbravo.utils.SystemConfigHolder;
import cn.com.dragonsoftbravo.utils.azure.AzureConstants;
import cn.com.dragonsoftbravo.utils.azure.model.UploadWASResult;
import cn.com.dragonsoftbravo.utils.azure.model.UserValidationResult;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.microsoft.azure.storage.blob.CloudBlockBlob;

public class WASService {

	private static String tokenValidationURL;
	private static String wasUploadURL;
	private static String cloudURL;

	private static boolean isPrd = SystemConfigHolder.instance().getValue("prd").equals("1");

	static {
		tokenValidationURL = SystemConfigHolder.instance().getValue("tokenValidationURL");
		wasUploadURL = SystemConfigHolder.instance().getValue("wasUploadURL");
		cloudURL = SystemConfigHolder.instance().getValue("cloudURL");
	}

	public static UserValidationResult validateToken(String userToken, int userTokenType, String userId) {

		if (!isPrd) {
			UserValidationResult result = new UserValidationResult();
			result.setCode(AzureConstants.UPLOAD_STATUS_NORMAL);
			result.setMessage("ok");
			return result;
		}

		// validate the token for the upload
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("methodName", "verifyToken");
		params.put("tokenCode", userToken);
		params.put("tokenType", userTokenType);
		params.put("userId", userId);

		HttpRequest validateRequest = HttpRequest.post(tokenValidationURL).form(params);
		HttpResponse validateResponse = validateRequest.send();
		String bodyText = validateResponse.bodyText();
		UserValidationResult validationResult = new Gson().fromJson(bodyText, UserValidationResult.class);

		return validationResult;
	}

	public static UploadWASResult uploadToWas(String campaignId, int fileType, String uploadToken, int uploadTokenType, String userId,
			long fileLength, CloudBlockBlob blob, Map<String, String> exifs) throws Exception {

		blob.downloadAttributes();

		if (!isPrd) {
			UploadWASResult result = new UploadWASResult();
			result.setCode(AzureConstants.UPLOAD_STATUS_NORMAL);
			result.setMessage("ok");
			return result;
		}

		Map<String, Object> params = new HashMap<String, Object>();
		// upload the information to WAS server
		params = new HashMap<String, Object>();
		params.put("methodName", "insertFileInfoFromCloud");
		params.put("campaignId", campaignId);
		params.put("fileType", fileType);
		params.put("uploadToken", uploadToken);
		params.put("uploadTokenType", uploadTokenType);
		params.put("userId", userId);
		params.put("fileUploadPath", cloudURL + blob.getName());

		if (0 == fileType) {
			params.put("thumbnailPath", cloudURL + blob.getMetadata().get(AzureConstants.THUMBNAIL_IMAGE_BLOB_NAME));
			params.put("fileDisplayPath", cloudURL + blob.getMetadata().get(AzureConstants.THUMBNAIL_IMAGE_BLOB_NAME2));
		} else {
			params.put("thumbnailPath", "http://media.sony.com.cn/campaignplatform/video/thumbnail.png");
			params.put("fileDisplayPath", cloudURL + blob.getName());
		}

		params.put("exif", new Gson().toJson(exifs, new TypeToken<Map<String, String>>() {
		}.getType()));

		// return a size in MB.
		params.put("fileSize", fileLength / (1024 * 1024));

		HttpRequest wasUploadRequest = HttpRequest.post(wasUploadURL).form(params);
		HttpResponse wasUploadResponse = wasUploadRequest.send();
		String bodyText = wasUploadResponse.bodyText();
		UploadWASResult uploadResult = new Gson().fromJson(bodyText, UploadWASResult.class);
		return uploadResult;
	}

}
